package com.nervenets.web.controller.handler.admin.user;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.controller.handler.common.TokenParams;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.entity.UserToken;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.enumeration.WealthType;
import com.nervenets.web.hibernate.domain.user.User;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Created by Joe on 2018/1/6.
 */
public class UserDetailForManagerHandler extends SessionHandler<UserDetailForManagerParams> {

    @Override
    public String handle(UserDetailForManagerParams params, HttpServletRequest request) throws Exception {
        User user = userDao.findById(params.getTargetUserId(), User.class);
        if (null == user) return errorMessage(MessageCode.code_404);
        long surplus = userDao.getSurplus(params.getTargetUserId(), WealthType.money);
        long total = globalService.getUserWealthOfTotalMoney(params.getTargetUserId(), WealthType.money);
        return successMessage(new Model(user, surplus, total, new UserToken(params.getTargetUserId(), 1).getTokenStr()));
    }

    @Override
    public String result() throws Exception {
        return null;
    }

    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    class Model {
        private User user;
        private long money;
        private long total;
        private String token;

        public Model(User user, long money, long total, String token) {
            this.user = user;
            this.money = money;
            this.total = total;
            this.token = token;
        }
    }
}
